// 课程相关

import axios from "axios";

// 获取课程
export function getCourses(type, courseType) {
    let params = {
        type
    }
    if (type === "canSelected") {
        params.courseType = courseType;
    }
    return axios({
        method: "get",
        url: "/api/courses",
        params,
        header: {
            Authorization: localStorage.getItem("Authorization")
        }
    }).then(response => {
        for (let i = 0; i < response.data.courses.length; i++) {
            response.data.courses[i].key = i;
        }
        return {
            result: true,
            courses: response.data.courses
        }
    }).catch(error => {
        return {
            result: false,
            msg: error.response.data.msg
        }
    })
}

// 获取讲台
export function getPlatforms(type, courseId) {
    return axios({
        method: "get",
        url: "/api/platforms",
        params:{
            type, courseId
        },
        header: {
            Authorization: localStorage.getItem("Authorization")
        }
    }).then( response => {
        for (let i = 0; i < response.data.platforms.length; i++) {
            response.data.platforms[i].key = i;
        }
        return {
            result: true,
            platforms: response.data.platforms
        }
    }).catch(error => {
        return {
            result: false,
            msg: error.response.data.msg
        }
    })
}

// 添加选课
export function addSelection(sessionId) {
    return axios({
        method: "post",
        url: "/api/selections",
        data: {
            sessionId
        },
        header: {
            Authorization: localStorage.getItem("Authorization")
        }
    }).then(response => {
        return {
            result: true,
            data: response.data
        }
    }).catch(error => {
        return {
            result: false,
            msg: error.response.data.msg
        }
    })
}

// 取消选课
export function deleteSelection(selectionId) {
    return axios({
        method: "delete",
        url: "/api/selections",
        params: {
            selectionId
        },
        header: {
            Authorization: localStorage.getItem("Authorization")
        }
    }).then(response => {
        return {
            result: true,
            data: response.data
        }
    }).catch(error => {
        return {
            result: false,
            msg: error.response.data.msg
        }
    })
}